using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OgilvyOne.Model;
using OgilvyOne.BLL;
using OgilvyOne.Utility;
namespace OgilvyOne.ProcessFlow
{
    public class StaffInfoController
    {
        // Fields
        private const string URL_MEMBERUPDATE = "member_modify.aspx";

        // Methods
        public StaffInfoInfo getStaffInfo(string staffCode, string password)
        {
            StaffInfo info = new StaffInfo();
            StaffInfoInfo info2 = info.getStaffInfo(staffCode, password);
            if (info2 != null)
            {
                string userHostAddress = HttpContext.Current.Request.UserHostAddress;
                int staffId = info2.StaffId;
                info.insertLoginLog(staffId, userHostAddress);
                HttpContext.Current.Session["STAFF_KEY"] = info2;
                HttpCookie cookie = new HttpCookie("wowCookie");
                HttpCookie cookie2 = HttpContext.Current.Request.Cookies["wowCookie"];
                if (cookie2 == null)
                {
                    cookie.Value = Cryptography.Encrypt(string.Concat(new object[] { info2.StaffId, ",", info2.StaffCode, ",", info2.Area, ",", info2.Province, ",", info2.City }));
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }
                HttpContext.Current.Session.Timeout = 0x57e40;
            }
            return info2;
        }

        public StaffInfoInfo getStaffSession()
        {
            StaffInfo info = new StaffInfo();
            return info.getStaffSession();
        }

        public bool insertStaffInfo(StaffInfoInfo info)
        {
            try
            {
                new StaffInfo().InsertDeal(info);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
            return true;
        }

        public bool UpdateLoginLog()
        {
            StaffInfoInfo info = this.getStaffSession();
            if (info == null)
            {
                return false;
            }
            string userHostAddress = HttpContext.Current.Request.UserHostAddress;
            int staffId = info.StaffId;
            new StaffInfo().updateLoginLog(staffId, userHostAddress);
            return true;
        }

        public void UpdateMember(StaffInfoInfo updatedMemberInfo)
        {
            new StaffInfo().Update_Member(updatedMemberInfo);
            HttpContext.Current.Response.Redirect("/member_modify.aspx", true);
        }

        public bool updateSkin(int staffId, int skin)
        {
            try
            {
                new StaffInfo().UpdateSkin(staffId, skin);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                return false;
            }
            return true;
        }

        public int UpdateStaffInfo(StaffInfoInfo staffInfoInfo)
        {
            StaffInfo info = new StaffInfo();
            HttpContext.Current.Session["STAFF_KEY"] = null;
            return info.UpdateStaffInfo(staffInfoInfo);
        }

        public int UpdateStaffPasswd(int staffId, string newPasswd, string oldPasswd, string email)
        {
            StaffInfo info = new StaffInfo();
            return info.UpdateStaffPasswd(staffId, newPasswd, oldPasswd, email);
        }

    }
}
